import { createRouter, createWebHistory } from 'vue-router'

import Home from '../views/Home.vue'
import { isAuthenticated } from '../utils/tools/auth.js'

// 导入所有tabbar组件
import Dashboard from '../components/tabbar/Dashboard.vue'
import KnowledgeManagement from '../components/tabbar/KnowledgeManagement/KnowledgeManagement.vue'
import KnowledgeBase from '../components/tabbar/KnowledgeBase.vue'
import QaSystem from '../components/tabbar/QaSystem.vue'
import PersonalCenter from '../components/tabbar/PersonalCenter.vue'
import AdminCenter from '../components/tabbar/AdminCenter.vue'
import UserManagement from '../components/tabbar/UserManagement.vue'
import DataAnalytics from '../components/tabbar/DataAnalytics.vue'
import SystemManagement from '../components/tabbar/SystemManagement.vue'

// 导航菜单配置
export const navigationConfig = [
  // 仪表盘 - 仅超级管理员可见
  {
    id: 'dashboard',
    name: '仪表盘',
    icon: 'fas fa-chart-pie',
    path: '/dashboard',
    component: Dashboard,
    roles: [1]
  },
  
  // 知识管理 - 普通用户和管理员可见
  {
    id: 'knowledge-management',
    name: '知识管理',
    icon: 'fas fa-book',
    path: '/knowledge-management',
    component: KnowledgeManagement,
    roles: [2, 3]
  },
  
  // 知识库构建 - 普通用户和管理员可见
  {
    id: 'knowledge-base',
    name: '知识库构建',
    icon: 'fas fa-database',
    path: '/knowledge-base',
    component: KnowledgeBase,
    roles: [2, 3]
  },
  
  // 智能问答 - 普通用户可见
  {
    id: 'qa-system',
    name: '智能问答',
    icon: 'fas fa-question-circle',
    path: '/qa-system',
    component: QaSystem,
    roles: [3]
  },
  
  // 个人中心 - 普通用户可见
  {
    id: 'personal-center',
    name: '个人中心',
    icon: 'fas fa-user',
    path: '/personal-center',
    component: PersonalCenter,
    roles: [3]
  },
  
  // 管理员中心 - 知识库管理员可见
  {
    id: 'admin-center',
    name: '管理员中心',
    icon: 'fas fa-user-shield',
    path: '/admin-center',
    component: AdminCenter,
    roles: [2]
  },
  
  // 用户管理 - 仅超级管理员可见
  {
    id: 'user-management',
    name: '用户管理',
    icon: 'fas fa-users',
    path: '/user-management',
    component: UserManagement,
    roles: [1]
  },
  
  // 数据分析 - 仅超级管理员可见
  {
    id: 'data-analytics',
    name: '数据分析',
    icon: 'fas fa-chart-line',
    path: '/data-analytics',
    component: DataAnalytics,
    roles: [1]
  },
  
  // 系统管理 - 仅超级管理员可见
  {
    id: 'system-management',
    name: '系统管理',
    icon: 'fas fa-cog',
    path: '/system-management',
    component: SystemManagement,
    roles: [1]
  }
]

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/home', name: 'home', component: Home, meta: { requiresAuth: true } },
  { path: '/login', name: 'login', component: () => import('../views/Login.vue') },
  { path: '/register', name: 'register', component: () => import('../views/Register.vue') },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

router.beforeEach((to) => {
  if (to.meta && to.meta.requiresAuth && !isAuthenticated()) {
    return { name: 'login', query: { redirect: to.fullPath } }
  }
  if ((to.name === 'login' || to.name === 'register') && isAuthenticated()) {
    return { name: 'home' }
  }
  return true
})

export default router


